<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ include file="/WEB-INF/jsp/common/taglibs.jsp" %> 
<script src="${appPath }/resource/js/validate_forminp.js" type="text/javascript" ></script>

<script>
function scrollToTop(){	
	//alert(document.body);	
	/*for ie*/
	document.documentElement.scrollTop = 0;
	//div.scrollTop = div.scrollHeight;
}

function _submit(istemp){	
	var f = document.getElementById("AgricultureAndFoodForm");
	f.action = "${appPath }/form/add.htm";
	if(istemp == 0 || validate()){
		$("#flow").val(istemp);
		var msg = (istemp == 0) ? "表單暫存" : "表單新增";		
		$.post("${appPath}/form/add.htm",$('#AgricultureAndFoodForm').serialize(),function(data){
			$("textarea[validate],input[validate],select[validate]").css({border:"1px solid #BDC7D8"});
			$("#_clazz").css({border:"0px solid #FFFFFF"});
			
			
			if(data.length == 6){			
				var _clazz = "";
				$('.clazz').each(function(){			
					if($(this).prop("checked")){
						_clazz += "<div>‧" + $(this).attr("v") + "</div>";
					}
				});	
				/*
				var _literature = "";
				$('.literature').each(function(){	
					if($(this).prop("checked")){
						var sValue = $(this).attr("v");
						if($(this).val() == 'D'){
							sValue += " : " + $("#literature_desc").val();
						}
						_literature += "<div>‧" + sValue + "</div>";
						
					}
				});		
				*/
				
				var _title_o = $("#title_o").val();
				var _title_c = $("#title_c").val();
				var _author  = $("#author").val();
				var _affiliation = $("#affiliation").val();
				var _publisher = $("#publisher").val();
				var _journal = $("#journal").val();
				var _vol_page = $("#vol_page").val();
				var _keyword_o = $("#keyword_o").val();	
				var _url  = $("#url").val();
				var _abstract_c = $("#abstract_c").val();	
				var _abstract_o = $("#abstract_o").val();	
				var _year = $('#year option:selected').text();
				var _language = $('#language option:selected').text();
				var _literature = $('#literature option:selected').text();				
				if($('#literature option:selected').val() == 4){
					_literature += " : " +  $("#literature_desc").val();	
				}
				
				var _doc_type = $('#doc_type option:selected').text();
				var _analyzed = $('.analyzed:checked').val() == "true" ? "有" : "無";				
				var _countries = $('#countries_name_ch').val();	
				var _plants_ch = $('#plants_name_ch').val();			
				_plants_ch = replaceAll(_plants_ch,",","、").substring(0,_plants_ch.length-1);
				_countries = replaceAll(_countries,",","、").substring(0,_countries.length-1);				
				$("#span_selectedCodeCountries").html(_countries);
				$("#span_selectedCodePlants").html(_plants_ch);				
				$("#show_title_o").html(_title_o);
				$("#show_title_c").html(_title_c);
				$("#show_author").html(_author);
				$("#show_affiliation").html(_affiliation);
				$("#show_publisher").html(_publisher);
				$("#show_journal").html(_journal);
				$("#show_vol_page").html(_vol_page);
				$("#show_keyword_o").html(_keyword_o);
				$("#show_url").html(_url);
				$("#show_abstract_c").html(_abstract_c);	
				$("#show_abstract_o").html(_abstract_o);
				$("#show_year").html(_year);
				$("#show_language").html(_language);
				$("#show_doc_type").html(_doc_type);	
				$("#show_clazz").html(_clazz);	
				$("#show_literature").html(_literature);				
				$("#show_analyzed").html(_analyzed);
				$(".editField").hide();
				$(".showField").show();
				$("#Span_Rec_No").html(data);
				scrollToTop();				
				showMessage(msg + "成功",1);
			}else{
				showMessage(msg + "失敗,請將錯誤編號貼至問題回報給管理員,謝謝! 錯誤編號:<span style='color: red;font-weight: 600;'>" + data + "</span>",2);
			}
		});
	}else{
		showMessage("尚有欄位未完成",2);
	}
}

function _continue(){
	document.getElementById("AgricultureAndFoodForm").reset();	
	$("#Span_Rec_No").html("編號尚未產生");	
	$('.clazz').each(function(){			
		$(this).attr("disabled",false);
	});	
	$("#span_selectedCodePlants").html("");
	$("#span_selectedCodeCountries").html("");	
	$("#upload_files_area").html("");	
	$("#plants_code").val("");
	$("#plants_name_ch").val("");
	$("#countries_code").val("");
	$("#countries_name_ch").val("");	
	$(".editField").show();
	$(".showField").hide();
}

function preview(rec){
	window.open("${appPath}/form/preview.htm?rec_no="+rec,"_blank" ,"height=500, width=620, top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no");
}

function showLiteratureDesc(obj){
	/*
	$("#AgricultureAndFoodForm input[name=literature_desc]").prop("disabled",!obj.checked);	
	document.getElementById("AgricultureAndFoodForm").elements["literature_desc"].focus();
	*/
	if(obj.value == 4){
		$("#AgricultureAndFoodForm input[name=literature_desc]").show();
	}else{
		$("#AgricultureAndFoodForm input[name=literature_desc]").hide();
	}
	
}
</script>

<div id="page">
	<div id="page-bgtop">
		<div id="page-bgbtm">
			<div id="content">
				<div class="post">
					<%-- 
					<h2 class="title inner">表單輸入</h2>					
					<div style="clear: both;">&nbsp;</div>
					--%>
					<div class="entry" >	
						<%@ include file="form.jsp" %>	
					</div>
				</div>
				<div style="clear: both;">&nbsp;</div>
			</div>
			<!-- end #content -->			
			<%@ include file="/WEB-INF/jsp/common/submenu02.jsp" %>			
			<div style="clear: both;">&nbsp;</div>
		</div>
	</div>
</div>
<!-- end #page -->